
{include file="webautocode@public/header"}

<style>
    .btn{
        width: 200px;
        height: 35px;
        border-radius: 15px;
    }
    .sp-left{
        display: inline-block;
        width: 200px;
        height: 35px;
        line-height: 35px;
        text-align: right;
    }
</style>

<form onsubmit="return false;" data-auto="true" method="post">
    <!--{empty name='$field_list'}-->
    <p class="help-block text-center well">没 有 记 录 哦！</p>
    <!--{else}-->
    <p class="help-block text-center well font-s20">【创建新增接口】</p>
    <div>
        <ul type="disc" style="margin: 20px">

            <li>********************************</li>
            <li>*生成接口时自动生成了接口文档</li>
            <li>*生成接口时自动加入了用户操作日志（修改、添加、上传等操作）</li>
            <li>* 1、选择需要页面输入的修改参数</li>
            <li>* 2、输入参数是否必须</li>
            <li>********************************</li>
            <li><span style="color: #0f74a8">添加新增api时，重点关注必须的字段列</span></li>
        </ul>
    </div>
    <div>

        <input type="text" style="width: 20%" value="{$api_code_root_path}" class="layui-input-block code_save_path" placeholder="代码存放物理路径(默认存放在当前项目的根目录)">
    </div>

    <div style="margin: 30px">
        接口控制器名:
        <input type="text" class="api_controller_name" style="width: 200px;height: 30px" value="{$apiControllerName}">
    </div>
    <div style="margin: 30px">
        <input type="text" class="api_name" style="width: 200px;height: 30px" placeholder="请指定接口名称"
               onblur="ShowFields.saveApiName(this)" value="add{$apiControllerName}Info">
        <input type="text" class="api_desc" style="width: 200px;height: 30px" value="新增{$table_comments}"
               placeholder="请说明接口用途">
    </div>
    <div style="margin: 30px">
        <input type="text" class="api_domain" style="width: 200px;height: 30px" placeholder="请指定接口访问域名">
    </div>

    <div style="margin: 30px;border-top: 2px solid gray;">

    </div>
    <table class="layui-table" lay-skin="line">
        <thead>
        <tr>
            <th class='text-left nowrap'></th>
            <th class='text-left nowrap'>[{$tablename}]字段名称</th>
            <th class='text-left nowrap'>选择需要新增的字段</th>
            <th class='text-left nowrap'>表单是否必须</th>
            <th class='text-left nowrap'>文件上传字段</th>
<!--            <th class='text-left nowrap'>固定字段值</th>-->
            <th class='text-left nowrap' style="border-left: 1px solid;">关联表同时新增</th>
        </tr>
        </thead>
        <tbody>
        <!--{foreach $field_list as $key=>$vo}-->
        <tr>
            <td class='list-table-check-td'>
                &nbsp;
            </td>
            <td class='text-left nowrap'>
                {$vo}
            </td>
            <td class='text-left nowrap'>
                <input class="list-check-box" name="field_name" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">
            </td>

            <td class='text-left nowrap'>
                <input class="list-check-box" name="is_must" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">
            </td>
            <td class='text-left nowrap'>
                <input class="list-check-box" name="file_upload_field" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">
            </td>

<!--            <td class='text-left nowrap'>-->
<!--                固定值<input class="" name="fix_field_val" data-field="{$vo}" value='' type='text' style="width: auto;height: 30px">-->
<!--            </td>-->
            <td class='text-left nowrap' style="border-left: 1px solid;">
                关联表新增

            </td>
        </tr>
        <!--{/foreach}-->
        </tbody>
    </table>
<!--    <div style="margin: 30px">-->
<!--        开启校验模式：-->
<!--        字段必须：<input type="radio" name="is_check" style="width: 30px;height: 30px" value="yes">-->
<!--        非必须：<input type="radio" name="is_check" style="width: 30px;height: 30px" value="no">-->
<!--    </div>-->

    <div style="margin: 30px">



    </div>
    <div style="margin: 30px" class="api_url">

    </div>
    <div style="margin: 50px">
        <button class="btn" onclick="ShowFields.createaddapi()">生成新增API</button>
    </div>
    
    <p style="height: 100px">

    </p>
    <!--{/empty}-->
    {include file='public/test_module'}
</form>
<script>
    let ShowFields = {
        _api_name:'add{$apiControllerName}Info',
        saveApiName: function (obj){
            this._api_name = $(obj).val();
        },
        //设置接口名称
        setFuncName:function (name){
            if(!this._api_name){
                $('.api_name').val(this._api_name+name);
            }
        },
        //执行创建api
        createaddapi: function (){
            let tablename='{$tablename}';
            let url = '{:url("createaddapi")}';
            //接口输入字段名
            let input_fields = $('input:checkbox[name="field_name"]:checked').map(function(){
                return $(this).val();
            }).get().join(',');
            if(!input_fields){
                return layer.msg('请选择输入字段名');
            }
            //接口域名
            let api_domain = $('.api_domain').val();
            let api_controller_name = $('.api_controller_name').val();
            if(!api_controller_name){
                return layer.msg('请指定接口控制器名');
            }
            let api_name = $('.api_name').val();
            if(!api_name){
                return layer.msg('请指定接口名');
            }
            //接口描述
            let api_desc = $('.api_desc').val();
            //是否必须
            let is_must = $('input:checkbox[name="is_must"]:checked').map(function(){
                return $(this).val();
            }).get().join(',');

            // let rel_table = $('select[name="rel_table"]').map(function(){
            //     return $(this).val();
            // }).get().join(',');


            //固定字段值
            let fix_field_val = $('input[name="fix_field_val"]').map(function(){
                return $(this).val()?$(this).attr('data-field')+'=='+$(this).val():'';
            }).get().join(',');
            //文件上传字段
            let file_upload_field = $('input:checkbox[name="file_upload_field"]:checked').map(function(){
                return $(this).val();
            }).get().join(',');
            let data = {
                tablename:tablename,
                input_fields:input_fields,
                api_name:api_name,//接口名称
                api_desc:api_desc,//接口说明
                //接口控制器名
                api_controller_name:api_controller_name,
                is_must:is_must,//是否必须
                //关联表（字段-表）
                api_domain:api_domain,
                fix_field_val:fix_field_val,//固定字段值
                code_save_path:$('.code_save_path').val(),//接口代码保存路径
                file_upload_field:file_upload_field,
            };

            $.post(url,data,function (res){
                layer.msg(res.msg);
                if(res.code != 200){
                    return ;
                }

                $('.api_url').html(api_domain+res.data.api_url)
            },'json');



        }
    }
</script>
